package com.cms.entity;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cms.entity.base.BaseForm;
import com.cms.util.DBUtils;
import com.jfinal.plugin.activerecord.Page;

/**
 * Entity - 表单
 * 
 * 
 * 
 */
@SuppressWarnings("serial")
public class Form extends BaseForm<Form> {
	
	/**
	 * 查找表单分页
	 * 
	 * @param modelId
	 *            模型ID
	 * @param pageNumber
	 *            页码
	 * @param pageSize
	 *            每页记录数
	 * @return 表单分页
	 */
	public Page<Form> findPage(Long modelId,Long contentId,Integer pageNumber,Integer pageSize,Long siteId){
	    String filterSql = " and siteId="+siteId;
		if(modelId!=null){
		    filterSql+=" and modelId = " + modelId;
		}
		if(contentId!=null){
		    filterSql+=" and contentId = " + contentId;
		}
		String orderBySql = DBUtils.getOrderBySql("createDate desc");
		return paginate(pageNumber, pageSize, "select *", "from cms_form where 1=1 "+filterSql+orderBySql);
	}
	
    /**
     * 获取属性
     * 
      @param name
     *          名称
     * @return 属性
     */
    public String getAttribute(String name){
        JSONObject jsonObject = JSONObject.parseObject(getModelFieldValue());
        return jsonObject.getString(name);
    }
    
   /**
     * 获取属性
     * 
      @param name
     *          名称
     * @return 属性
     */
    public List<String> getAttributes(String name){
        JSONObject jsonObject = JSONObject.parseObject(getModelFieldValue());
        JSONArray jsonArray = jsonObject.getJSONArray(name);
        if(jsonArray == null){
            return new ArrayList<>();
        }
        return JSONArray.parseArray(jsonArray.toJSONString(),String.class);
    }
}
